/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React from 'react';
import routesMap from '@/utils/oldRoutesMap';
import { useLocation, useSelector } from 'umi';
import type { ConnectState } from '@/models/connect';
import './_utils/crypto';
import styles from './index.less';

interface IframeProps {
  pathname?: string;
}

function getOldRoute(pathname: string): string {
  const oldRoute = routesMap[`${pathname}`.replace(/^\/f\//, '')] || '';
  return oldRoute;
}

const Iframe: React.FC<IframeProps> = (props) => {
  const { pathname = '' } = props;
  const location = useLocation();
  const { winSize } = useSelector((state: ConnectState) => state.global);
  const { pathname: locationPathname, search = '' } = pathname ? { pathname } : location;
  const host = '//admin.kuaidihelp.com';
  const oldRoute = getOldRoute(locationPathname);
  const Connector = search ? '&' : '?';
  const url = oldRoute ? `${host}/${oldRoute}${search}${Connector}winSize=${winSize}` : '';

  return url ? (
    <div className={styles.screenBox}>
      <div className={styles.scrollX}>
        <iframe
          id='iframe-box'
          src={url}
          width='1200px'
          height='100%'
          // scrolling="no"
          frameBorder='0'
          className={styles.iframe}
          name={pathname}
        />
      </div>
    </div>
  ) : (
    <>{props.children}</>
  );
};

export default Iframe;
